import {addCancelMember, lookCancelMember, delCancelMember} from '../../../utils/api.js' //dateTime2 dateTimeArray1
Page({
  data: {
    arrayList: [],
    phone: null,
    name: null,
    showModal: false,
  },

  //表单提交
  formSubmit(e) {
    const { phone, name } = this.data;
    if (!phone) {
      wx.showToast({
        title: "电话号码不能为空",
        icon: 'none',
        duration: 1000
      });
      return
    }
    if (!name) {
      wx.showToast({
        title: "姓名不能为空",
        icon: 'none',
        duration: 1000
      });
      return
    }
    wx.showLoading({ title: '数据已提交' });
    addCancelMember({phone, name}).then(res => {
      wx.hideLoading();
      if (res.code === 617) {
        this.setData({
          phone: null,
          name: null,
          showModal: false,
        });
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 2000
        });
        this.getList(false, true);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'warn',
          duration: 2000
        })
      }
    })
  },
  cancelModal() {
    this.setData({
      showModal: false,
      phone: null,
      name: null,
    });
  },
  showModal() {
    this.setData({
      showModal: true,
      phone: null,
      name: null,
    });
  },
  bindNameInput: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindPhoneInput: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  /**
   * 获取活动或商品列表
   * @param save 是否保留已经存在的数据， 默认保留
   * @param abort 是否中断上一次未返回结果的请求， 默认不中断
   * */
  getList(save = true, abort = false) {
    wx.showLoading({ title: '正在获取数据...' });
    wx.stopPullDownRefresh();
    lookCancelMember({abort}).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        this.setData({
          arrayList: save ? this.data.arrayList.concat(res.datas) : res.datas,
        });
      } else {
        this.setData({
          arrayList: []
        });
        wx.showToast({
          title: res.msg,
          icon: 'warn',
          duration: 1000
        })
      }
    });
  },

  /**
   *
   * 删除核销员
   * */
  delCancelMember(e) {
    const id = e.currentTarget.dataset.id;
    delCancelMember({id}).then(res => {
      wx.hideLoading();
      if (res.code === 618) {
        this.getList(false, true)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'warn',
          duration: 1000
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
      this.getList(false, true);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getList(false, true)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (res) {
  },
});